package com.iyundao.io.controller;

import com.alibaba.fastjson.JSONObject;
import com.iyundao.io.Service.StatisticsService;
import com.iyundao.io.model.PartyMember;
import com.iyundao.io.model.StatisticsModel;
import com.iyundao.io.model.SysUserProvider;
import com.iyundao.io.tools.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/statistic")
public class StatisticController {

    @Autowired
    private StatisticsService statisticsService;

    @ApiOperation(value = "根据用户查询权限下所有组织", notes = "根据用户查询权限下所有组织")
    @RequestMapping(value = "/selectallorganizationid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData selectAllOrganizationId() {
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        return statisticsService.selectAllOrganizationId(sysUser);
    }

    @ApiOperation(value = "统计权限下所有活动，按月", notes = "统计权限下所有活动，按月")
    @RequestMapping(value = "/selectgroupbymonth", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public List<StatisticsModel> selectGroupByMonth(@RequestBody String params) {
        /*SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String sid = sysUser.getSysDept().getSid();*/
        JSONObject object = JSONObject.parseObject(params);
        String id = (String) object.get("id");
        return statisticsService.selectGroupByMonth(id);
    }

    @ApiOperation(value = "统计具体组织下所有活动，按月", notes = "统计具体组织下所有活动，按月")
    @RequestMapping(value = "/selectgroupbyorganizationid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData selectGroupByOrganizationId(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        String sid = (String) object.get("organizationId");
        return statisticsService.selectGroupByOrganizationId(sid);
    }

    @ApiOperation(value = "统计所有活动标题，按月", notes = "统计所有活动，按月")
    @RequestMapping(value = "/selectbymonth", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public List<StatisticsModel> selectByMonth(@RequestBody String params) throws ParseException {
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String sid = sysUser.getSysDept().getSid();
        JSONObject object = JSONObject.parseObject(params);
        String date = (String) object.get("date");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = format.parse(date);
        return statisticsService.selectByMonth(date1,sid);
    }

    @ApiOperation(value = "活动未到人员", notes = "活动未到人员")
    @RequestMapping(value = "/selectnotjoin", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public List<PartyMember> selectNotJoin(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer) object.get("id");
        return statisticsService.selectNotJoin(id);
    }

    @ApiOperation(value = "考评统计排名", notes = "考评统计排名")
    @RequestMapping(value = "/ratio", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData statistics(@RequestBody String params) {
        /*SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String sid = sysUser.getSysDept().getSid();*/

        JSONObject object = JSONObject.parseObject(params);
        String date = (String) object.get("date");
        String date1 = (String) object.get("date1");
        String id = (String) object.get("id");

        return statisticsService.statistics(id,date,date1);
    }

    @ApiOperation(value = "查询组织下人员信息", notes = "查询组织下人员信息")
    @RequestMapping(value = "/selectpersonal", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public  ResponseData selectBySid(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        String id = (String) object.get("id");
        return statisticsService.selectByOrganizationId(id);
    }
}
